// import Vue from 'vue'
// import Router from 'vue-router'
import homerouter from "./home";
import customerrouter from "./customer";
import userrouter from "./user";
import orderrouter from "./order";
import activityrouter from "./activity";
import cartrouter from "./cart";
import { Storage } from "../service/storage";

Vue.use(VueRouter);
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }
const router = new VueRouter({
  //重定向呢
  routes: [
    ...cartrouter,
    ...homerouter,
    ...userrouter,
    ...customerrouter,
    ...userrouter,
    ...orderrouter,
    ...activityrouter
  ]
});

router.beforeEach((to, from, next) => {
  let json = to.query;
  if (JSON.stringify(json) != "{}") {
    for (let key in json) {
      if (json[key]) {
        let t = json[key].toString();
        if (t.indexOf("?") != -1) {
          let arr = json[key].split("?");
          json[key] = arr[0];
        }
      }
    }
  }
  if (to.query.from == "app") {
    Storage.set("isInApp", true);
    /* Vue.$vux.confirm.show({
       title: to.query.Authorization+"=====",
       onCancel: () => {

       },
       onConfirm: (e) => {

       }
     })*/
    //Vue.$vux.toast.text(window.location.href, 'center');
    if (to.query.Authorization) {
      Storage.set("token", to.query.Authorization);
    }
  }
  if (to.meta.auth !== false && !Storage.get("token")) {
    next({
      path: "/login",
      query: { sellerId: to.query.sellerId || to.query.id }
    });
    return;
  }
  next();
});

export default router;
